﻿Imports System.Windows.Forms
Imports System.Drawing
Imports System.Diagnostics
Imports System.IO
Imports System.Xml.Serialization
Imports System.Collections
Imports System.Linq
Imports System.Xml.Linq

Public Class AMsg

    Protected Friend dsp_Title As String = ""
    Protected Friend dsp_Text As String = ""
    Protected Friend dsp_MsgBoxStyle As MsgBoxStyle
    Protected Friend iInnerException As Exception = Nothing
    Protected Friend iDataObject As Object = Nothing

    Overloads Shared Function ShowDialog(Text As String, _
                                         MsgBoxStyle As MsgBoxStyle, _
                                         Optional InnerException As Exception = Nothing, _
                                         Optional DataObject As Object = Nothing) As DialogResult
        Dim msg As New AMsg
        msg.dsp_Title = ""
        msg.dsp_Text = Text
        msg.dsp_MsgBoxStyle = MsgBoxStyle
        msg.iInnerException = InnerException
        msg.iDataObject = DataObject
        msg.DialogResult = Windows.Forms.DialogResult.Cancel
        Return msg.ShowDialog(Text)
    End Function

    Private Overloads Function ShowDialog(Text As String) As DialogResult

        SetTitle()

        CalculateWidthHeightMakeLayout()

        SetupMainDspText()

        SetupIconImage()

        ShowHideRequiredButtons()

        Return Me.ShowDialog()
    End Function

    Private Sub SetTitle()
        If Me.dsp_Title = "" Then
            Me.Text = Application.CompanyName & " " & Application.ProductName & " " & Application.ProductVersion.ToString
        Else
            Me.Text = Me.dsp_Title
        End If
    End Sub

    Private Sub CalculateWidthHeightMakeLayout()
        Me.StartPosition = FormStartPosition.CenterParent
        Dim maxWidth As Integer = My.Computer.Screen.Bounds.Width / 2 '2.59
        Dim maxHeight As Integer = My.Computer.Screen.Bounds.Height / 3

        Dim layoutsize As SizeF = New SizeF(maxWidth - (110), 5000.0)
        Dim g As Graphics = Graphics.FromHwnd(dsp_Label.Handle)
        Dim StringSize As SizeF = g.MeasureString(Me.dsp_Text, Me.Font, layoutsize)
        Dim rHeight As Integer = StringSize.Height

        'Fixup if too low
        If rHeight < 64 Then rHeight = 64
        'Fixup if too high
        If rHeight > maxHeight Then rHeight = maxHeight

        'Arrange layout
        If IsNothing(Me.iInnerException) = False Then
            Me.Size = New Size(maxWidth, rHeight + 150)
            More_ImageButton.Visible = True
        Else
            Me.Size = New Size(maxWidth, rHeight + 120)
            More_ImageButton.Visible = False
        End If
        Me.MaximumSize = Me.Size
        Me.MinimumSize = Me.Size
        Me.previousSize = Me.Size
        dsp_Label.Size = New Size(maxWidth - 110, rHeight)
    End Sub

    Private Sub SetupMainDspText()
        Me.dsp_Label.Text = Me.dsp_Text
    End Sub

    Private Sub SetupIconImage()
        Select Case Me.dsp_MsgBoxStyle
            Case Is = (Me.dsp_MsgBoxStyle Or MsgBoxStyle.Exclamation)
                Me.dsp_PictureBox.Image = Me.ImageList1.Images(2)
            Case Is = (Me.dsp_MsgBoxStyle Or MsgBoxStyle.Critical)
                Me.dsp_PictureBox.Image = Me.ImageList1.Images(3)
            Case Is = (Me.dsp_MsgBoxStyle Or MsgBoxStyle.Question)
                Me.dsp_PictureBox.Image = Me.ImageList1.Images(4)
            Case Is = (Me.dsp_MsgBoxStyle Or MsgBoxStyle.Information)
                Me.dsp_PictureBox.Image = Me.ImageList1.Images(5)
            Case Else
                Me.dsp_PictureBox.Image = Me.ImageList1.Images(2)
        End Select
    End Sub

    Private Sub ShowHideRequiredButtons()
        Select Case Me.dsp_MsgBoxStyle
            Case Is = MsgBoxStyle.Exclamation
                OkOnly_TableLayoutPanel.Visible = True
                YesNo_TableLayoutPanel.Visible = False
                'OkOnly_TableLayoutPanel.Location = New Point(Me.Size.Width - 110, OkOnly_TableLayoutPanel.Location.Y)
            Case Is = MsgBoxStyle.Critical
                OkOnly_TableLayoutPanel.Visible = True
                YesNo_TableLayoutPanel.Visible = False
                'OkOnly_TableLayoutPanel.Location = New Point(Me.Size.Width - 110, OkOnly_TableLayoutPanel.Location.Y)
            Case Is = MsgBoxStyle.Question
                OkOnly_TableLayoutPanel.Visible = False
                YesNo_TableLayoutPanel.Visible = True
                'YesNo_TableLayoutPanel.Location = New Point(Me.Size.Width - 198, YesNo_TableLayoutPanel.Location.Y)
            Case Is = MsgBoxStyle.Information
                OkOnly_TableLayoutPanel.Visible = True
                YesNo_TableLayoutPanel.Visible = False
                'OkOnly_TableLayoutPanel.Location = New Point(Me.Size.Width - 110, OkOnly_TableLayoutPanel.Location.Y)
            Case Else
                OkOnly_TableLayoutPanel.Visible = True
                YesNo_TableLayoutPanel.Visible = False
                'OkOnly_TableLayoutPanel.Location = New Point(Me.Size.Width - 110, OkOnly_TableLayoutPanel.Location.Y)
        End Select
    End Sub

    Private Sub OkOnly_Ok_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles OkOnly_Ok_ImageButton.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub YesNo_Yes_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles YesNo_Yes_ImageButton.Click
        Me.DialogResult = Windows.Forms.DialogResult.Yes
        Me.Close()
    End Sub

    Private Sub YesNo_No_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles YesNo_No_ImageButton.Click
        Me.DialogResult = Windows.Forms.DialogResult.No
        Me.Close()
    End Sub

    Private previousSize As Size = Nothing
    Private previoudLocation As Point = Nothing
    Private Sub More_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles More_ImageButton.Click
        If More_ImageButton.Text = "More..." Then
            More_ImageButton.Text = "Less..."
            More_ImageButton.ImageIndex = 7
            Me.MinimumSize = New Size(0, 0)
            Me.MaximumSize = Me.MinimumSize
            Me.previoudLocation = Me.Location
            Me.Location = New Point(0, 0)
            Me.previousSize = Me.Size
            Me.Size = New Size(My.Computer.Screen.WorkingArea.Width, My.Computer.Screen.WorkingArea.Height)
            debug_RichTextBox.Visible = True
            ExportDebugReport_ImageButton.Visible = True
            Debug.Write(GetDebugXML())
            debug_RichTextBox.Text = GetDebugXML()
        Else
            More_ImageButton.Text = "More..."
            More_ImageButton.ImageIndex = 6
            Me.Size = Me.previousSize
            Me.Location = Me.previoudLocation
            Me.MinimumSize = Me.Size
            Me.MaximumSize = Me.MinimumSize
            debug_RichTextBox.Visible = False
            ExportDebugReport_ImageButton.Visible = False
        End If
    End Sub

    Private Function GetDebugXML() As String
        Dim dh As New Abyx.DebugHelper.DebugHelper(Me.dsp_Text, Me.iInnerException, Me.iDataObject)
        Dim xml As String = ""

        Try
            xml = dh.GetSerializedXML()
        Catch ex As Exception
            xml = "Error generating error report.... report to system " & _
                "administrator with the following details" & vbNewLine & ex.ToString
        End Try

        Return dh.GetSerializedXML
    End Function

    Private Sub ExportDebugReport_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles ExportDebugReport_ImageButton.Click
        If SaveXmlFile_SaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim fName As String = SaveXmlFile_SaveFileDialog.FileName
            debug_RichTextBox.SaveFile(fName, RichTextBoxStreamType.UnicodePlainText)
        End If
    End Sub

End Class

'Public Class AppErrorInformation(Of T)

'    Private iMessage As String = ""
'    Private iErrorObject As Exception = Nothing
'    Private iDataObject As T = Nothing
'    Public Property Message As String
'        Get
'            Return Me.iMessage
'        End Get
'        Set(value As String)
'            Me.iMessage = value
'        End Set
'    End Property
'    Public Property ErrorObject As Exception
'        Get
'            Return Me.iErrorObject
'        End Get
'        Set(value As Exception)
'            Me.iErrorObject = value
'        End Set
'    End Property
'    Public Property DataObject As T
'        Get
'            Return Me.iDataObject
'        End Get
'        Set(value As T)
'            Me.iDataObject = value
'        End Set
'    End Property

'    Private Sub New()
'    End Sub
'    Public Sub New(Message As String, _
'                   Optional ErrorObject As Exception = Nothing, _
'                   Optional DataObject As T = Nothing)

'    End Sub

'End Class